package com.mckillion.icare;

//The purpose of this class is to show the splash screen.

//Import the required java packages.
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.WindowManager;
import com.mckillion.icare.R;

public class SplashCode extends Activity{
	
	//Declare a boolean to be used in the try catch block.
	private boolean isActive = true;
	
	//Declare an integer which will determine the length of the splash.
	private int splashTime = 5000;
	
	private SharedPreferences prefs;
	private String homeLat, homeLong, radius, inactiveMinText;
	
	//Override onCreate so that the code will run when the activity is started.
	@Override
    public void onCreate(Bundle savedInstanceState) {
        //Call to the super class.
		super.onCreate(savedInstanceState);		
		
        //Remove title and notification bars to give it an android feel.
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        //Set the content layout to the splash_screen XML file.
        setContentView(R.layout.splash_screen);
               
        //If any of the preferences are equal to null, reset them to default - This will allow the Application to be reopened if the user enters null
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        
        homeLat = prefs.getString("home_lat_pref","0");
    	homeLong = prefs.getString("home_long_pref","0");
    	radius = prefs.getString("fence_range","0");
    	inactiveMinText = prefs.getString("inactivity_length", "0");
        
    	if(inactiveMinText == "")
        {	inactiveMinText = "1440"; //Set the default to 24 hours if null is entered.
	        SharedPreferences.Editor editor = prefs.edit();
	        editor.putString("fence_range", inactiveMinText);
	        editor.commit();
        }
    	
    	if(homeLat == "")
        {	homeLat = "54.1"; //Set the default to Belfast if null is entered.
	        SharedPreferences.Editor editor = prefs.edit();
	        editor.putString("home_lat_pref", homeLat);
	        editor.commit();
        }
    	
    	if(homeLong == "")
        {	homeLong = "5.1"; //Set the default to Belfast if null is entered.
	        SharedPreferences.Editor editor = prefs.edit();
	        editor.putString("home_long_pref", homeLong);
	        editor.commit();
        }
    	
    	if(radius == "")
        {	radius = "40075020f"; //Set the default to the size of the world if null is entered.
	        SharedPreferences.Editor editor = prefs.edit();
	        editor.putString("fence_range", radius);
	        editor.commit();
        }
        
        //As the splash screen is going to be timed a Thread is declared.
        Thread splashThread = new Thread() 
        
        { 	//Overriding the run method means that this code will run when the onCreate method is called.
        	@Override
        	public void run() 
        	{   //Use a try-catch block to show the screen for the declared time.
        		try 
        		{	int waited = 0; 							//Used to determine how long the Thread has been running.
        			while(isActive && (waited < splashTime)) 	//If the waited time is less that splashTime keep showing the splash
        			{	sleep(100);								//Sleep for a tenth of a second before re-checking the while.
        				if(isActive) 
        				{	waited+=100;						//Add a tenth of a second to waited until waited is not less than splashTime.
        				}
        			}
        		}	
        		
        		catch(InterruptedException e) 
        			{	//Do nothing
        			}	
        		
        		finally 
        				{	finish();						//Finish the current activity.
		        			Intent i = new Intent();		//Create a new intent.
		        			i.setClassName("com.mckillion.icare", "com.mckillion.icare.HomeScreen"); //Use the intent to move to MainScreen.
		        			startActivity(i);				//Start the intent to initiate MainScreen.
        				}
        			}
        };
        
        splashThread.start();	//Start the Thread within onCreate so that it runs until the MainScreen class is called.
        
    }
}